package cn.sdhuijin.mediaplatform;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.Toast;
import cn.sdhuijin.mediaplatform.service.SynService;
import cn.sdhuijin.mediaplatform.update.UpdateManager;

/**
 * @author sunp@sdhuijin.cn:
 * @version 创建时间：2015年3月28日 上午10:50:28 类说明
 */
public class SeetingActivity extends PreferenceActivity implements
		Preference.OnPreferenceClickListener {

	private SharedPreferences prefs;
	private EditTextPreference editIp;
	private EditTextPreference editPort;
	private EditTextPreference editUser;
	private EditTextPreference editPWD;
	private Preference update;
	private Preference updateSoftware;
	private Preference fileController;

	@SuppressWarnings("deprecation")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.setting_preference);

		prefs = PreferenceManager.getDefaultSharedPreferences(this);
		editIp = (EditTextPreference) findPreference("edt_ip");
		editPort = (EditTextPreference) findPreference("edt_port");
		editUser = (EditTextPreference) findPreference("edt_user");
		editPWD = (EditTextPreference) findPreference("edt_pwd");
		update = (Preference) findPreference("btn_update");
		updateSoftware = (Preference) findPreference("btn_softupdate");
		fileController = (Preference) findPreference("btn_filecontroller");

		editIp.setSummary(prefs.getString("edt_ip", "请点击设置ip"));
		editPort.setSummary(prefs.getString("edt_port", "请点击设置端口"));
		editUser.setSummary(prefs.getString("edt_user", "请点击设置用户名"));
		editPWD.setSummary(prefs.getString("edt_pwd", "请点击设置密码"));

		update.setOnPreferenceClickListener(this);
		updateSoftware.setOnPreferenceClickListener(this);
		fileController.setOnPreferenceClickListener(this);

		editIp.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			@Override
			public boolean onPreferenceChange(Preference preference,
					Object newValue) {
				editIp.setSummary(newValue.toString());
				editIp.setDefaultValue(newValue);
				editIp.setText(newValue.toString());
				return false;
			}
		});

		editPort.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			@Override
			public boolean onPreferenceChange(Preference preference,
					Object newValue) {
				editPort.setSummary(newValue.toString());
				editPort.setDefaultValue(newValue);
				editPort.setText(newValue.toString());
				return false;
			}
		});

		editUser.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			@Override
			public boolean onPreferenceChange(Preference preference,
					Object newValue) {
				editUser.setSummary(newValue.toString());
				editUser.setDefaultValue(newValue);
				editUser.setText(newValue.toString());
				return false;
			}
		});

		editPWD.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			@Override
			public boolean onPreferenceChange(Preference preference,
					Object newValue) {
				editPWD.setSummary(newValue.toString());
				editPWD.setDefaultValue(newValue);
				editPWD.setText(newValue.toString());
				return false;
			}
		});
	}

	@Override
	public boolean onPreferenceClick(Preference preference) {
		if (preference == update) {
			Log.e("内容", "开始更新");
			SynService syn = new SynService();
			syn.onStart(this);
		} else if (preference == updateSoftware) {
			UpdateManager manager = new UpdateManager(SeetingActivity.this);
			manager.checkUpdate();
		} else if (preference == fileController) {
			PackageInfo packageInfo;
			try {
				packageInfo = this.getPackageManager().getPackageInfo(
						"com.android.rk", 0);
			} catch (NameNotFoundException e) {
				packageInfo = null;
				e.printStackTrace();
			}
			if (packageInfo == null) {
				Toast.makeText(this, "文件管理暂时不可用！", Toast.LENGTH_LONG).show();
			} else {
				Intent intent = new Intent();
				intent.setComponent(new ComponentName("com.android.rk",
						"com.android.rk.RockExplorer"));
				intent.setAction(Intent.ACTION_VIEW);
				startActivity(intent);
			}
		}
		return false;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Intent intent = new Intent(SeetingActivity.this, MainActivity.class);
			SeetingActivity.this.startActivity(intent);
			return true;
		} else
			return super.onKeyDown(keyCode, event);
	}
}
